/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.util;

import com.bifrost.constant.PricingModel;
import com.bifrost.constant.ProductSkuType;
import com.bifrost.entity.*;
import com.bifrost.model.ProductModel;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/10/26 17:05
 */
public class SkuTransformUtils {
    public static ProductModel transformToProductModel(ProductSku sku) {
        if (sku == null) {
            return null;
        } else {
            //TODO 判断sku是否可用
            ProductModel model = new ProductModel();
            model.setSku(sku.getSkuId());
            ProductBrand productBrand = sku.getProduct().getProductBrand();
            if (productBrand != null) {
                model.setBrand(productBrand.getBrandName());
                Media brandLogo = productBrand.getBrandLogo();
                String brandImg = brandLogo == null ? "" : brandLogo.getUrl();
                model.setBrandImage(StringUtils.isBlank(brandImg) ? "" : brandImg);
            }
            model.setName(sku.getSkuName());
            model.setDescription(sku.getSkuDescription());
            String unit = sku.getSpecification() + sku.getSpecificationUnit();
            model.setUnit(StringUtils.isBlank(unit) ? "" : unit);
            model.setCanAddCart(!Boolean.FALSE.equals(sku.getCanAddCart()));
            if (ProductSkuType.BUNDLE.equalsIgnoreCase(sku.getItemType())) {
                if (PricingModel.BUNDLE.equalsIgnoreCase(sku.getPricingModel())) {
                    BigDecimal price = BigDecimal.ZERO;
                    List<SkuBundleItem> bundleItems = sku.getSkuBundleItems();
                    for (SkuBundleItem skuBundleItem : bundleItems) {
                        BigDecimal itemPrice = skuBundleItem.getItemPrice();
                        if (itemPrice != null) {
                            price = price.add(itemPrice.multiply(BigDecimal.valueOf(skuBundleItem.getBundleQuantity())));
                        }
                    }
                    model.setSalePrice(sku.getSalePrice());
                    model.setRetailPrice(price);
                } else {
                    List<SkuBundleItem> bundleItems = sku.getSkuBundleItems();
                    BigDecimal price = BigDecimal.ZERO;
                    for (SkuBundleItem skuBundleItem : bundleItems) {
                        BigDecimal itemPrice = skuBundleItem.getItemPrice();
                        if (itemPrice != null) {
                            price = price.add(itemPrice.multiply(BigDecimal.valueOf(skuBundleItem.getBundleQuantity())));
                        }
                    }
                    model.setRetailPrice(price);
                    model.setSalePrice(price);
                }
            } else {
                model.setSalePrice(sku.getSalePrice());
            }
            model.setRetailPrice(sku.getRetailPrice());
            model.setCount(BigDecimal.valueOf(1));
            List images = new ArrayList();
            model.setImages(images);
            Category category = sku.getCategory();
            model.setCategoryId(category.getCategoryId());
            model.setCategoryName(category.getCategoryName());
            List<SkuMediaMap> medias = sku.getSkuMediaMaps();
            for (SkuMediaMap mediaMap : medias) {
                if ("PRIMARY".equalsIgnoreCase(mediaMap.getMapKey())) {
                    String url = mediaMap.getMedia().getUrl();
                    if (model.getImage() == null) {
                        model.setImage(url);
                    }
                    images.add(url);
                }

            }
            model.setGifts(new ArrayList());
            model.setTags(new ArrayList());
            model.setShippingType(sku.getFulfillmentType());
            //TODO 附加sku的赠品
            return model;
        }
    }
}
